package btree;

import btree.base.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution_199 {
    /**
     * 使用BFS的时候，一定要对root==null单独判断
     */
    public List<Integer> rightSideView(TreeNode root) {
        if (root == null)
            return new ArrayList<>();
        List<Integer> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int len = queue.size();
            int tmp = 0;
            for (int i = 0; i < len; i++) {
                TreeNode treeNode = queue.poll();
                tmp = treeNode.val;
                if (treeNode.left != null)
                    queue.offer(treeNode.left);
                if (treeNode.right != null)
                    queue.offer(treeNode.right);
            }
            res.add(tmp);
        }
        return res;
    }
}
